package com.xty.itgroup.tech05tytex.Controller;


import com.xty.itgroup.tech05tytex.Databean.BeanException;
import com.xty.itgroup.tech05tytex.Util.Result;
import com.xty.itgroup.tech05tytex.Util.ResultUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 应用到所有@RequestMapping注解方法，在其执行之前初始化数据绑定器
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
    }

    /**
     * 把值绑定到Model中，使全局@RequestMapping可以获取到该值
     *
     * @param model
     */
    @ModelAttribute
    public void addAttributes(Model model) {
        model.addAttribute("author", "jhn");
    }

    /**
     * 全局异常捕捉处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BeanException.class)
    @ResponseBody
    public Result handlerSellerException(BeanException e) {
        return ResultUtil.error(e.getCode(), e.getMsg());
    }

}
